package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
@Slf4j
public class CheckItemController {
    //注入service层
    @DubboReference
    private CheckItemService checkItemService;

    /**
     * 检查项-分页查询
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping("findPage")
    //添加权限管理
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    public Result findPage(QueryPageBean queryPageBean) {
        log.info("[检查项---分页查询]data: {}", queryPageBean);
        //1.调用远程的rpc完成查询
        PageResult pageResult = checkItemService.findPage(queryPageBean);
        //2.返回封装的结果
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, pageResult);
    }

    /**
     * 检查项--添加检查项功能
     *
     * @param checkItem
     * @return
     */
    @PostMapping("add")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_ADD')")
    public Result add(@RequestBody CheckItem checkItem) {
        log.info("[检查项---添加查询]data: {}", checkItem);
        checkItemService.add(checkItem);
        return new Result(true, MessageConst.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * 检查项--删除检查项功能
     *
     * @param id
     * @return
     */
    @PostMapping("delete")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_DELETE')")
    public Result delete(Integer id) {
        log.info("[检查项---删除检查项]data: {}", id);
        checkItemService.delete(id);
        return new Result(true, MessageConst.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 根据id查询检查项
     *
     * @param id
     * @return
     */
    @GetMapping("findById")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    public Result findById(Integer id) {
        log.info("[检查项---根据id查询检查项]data: {}", id);
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, checkItem);
    }

    /**
     * 检查项目--根据id修改数据
     *
     * @param checkItem
     * @return
     */
    @PostMapping("edit")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_EDIT')")
    public Result edit(@RequestBody() CheckItem checkItem) {
        log.info("[检查项-根据id修改数据]data: {}", checkItem);
        checkItemService.edit(checkItem);
        return new Result(true, MessageConst.EDIT_CHECKITEM_SUCCESS);
    }
    /**
     * @Author: liangWeiQUQ 
     * @Date: 14:56 2021/2/27
     * @Parms 
     * @ReturnType: 
     * @Description: 检查项--查询所有项目
    */
    @GetMapping("findAll")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    public Result findAll() {
        log.info("[检查项-查询所有数据]");
        List<CheckItem> checkItemList = checkItemService.findAll();
        return new Result(true, MessageConst.QUERY_CHECKITEM_SUCCESS, checkItemList);
    }
}
